/**********************************************************************
 Copyright (C) 2009 Jaakko Vuori

 This file is part of Cimer.
 For more information, see <http://code.google.com/p/cimer/>

 Cimer is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Cimer is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Cimer.  If not, see <http://www.gnu.org/licenses/>.
 **********************************************************************/

#ifndef PIEWIDGET_H
#define PIEWIDGET_H

#include <QtGui/QWidget>
#include <QtCore/QTimer>
#include <QEvent>
#include <QTime>
#include <QLabel>

class KeyEventHandler;
class FeedbackPlayer;
class PieWidget : public QWidget
{
    Q_OBJECT

public:
    PieWidget(QWidget* parent = NULL);
    int secondsLeft() const;
    bool getLockValue();
    void showLockedMessage();

Q_SIGNALS:
    void timeAdjusted(int secondsLeft);
    void timeElapsed();
    void timeChanged(QString text);

public Q_SLOTS:
    void setTime(QTime time);
    void adjustTime(int minutes);
    void setPaused(bool paused);
    void enableLock(bool enabled);

private slots:
    void handleTick();

protected:
    void paintEvent(QPaintEvent* event);
    void mousePressEvent(QMouseEvent* event);
    void mouseReleaseEvent(QMouseEvent* event);
    void mouseMoveEvent(QMouseEvent* event);
    void keyPressEvent(QKeyEvent* event);
    void keyReleaseEvent(QKeyEvent* event);

private:
    void movePointer(QMouseEvent* event);
    void handleNewAngle(int angle);
    void prepareToRepaint(int seconds);
    void setZero();
    qreal degrees(qreal x, qreal y) const;
    void animatedHide(QLabel* label);
    int getPointSize();

private:
    int m_angle;
    int m_previousAdjustedAngle;
    QString m_text;
    QTimer m_tickTimer;
    int m_startSeconds;
    int m_secondsLeft;
    QDateTime m_startTime;
    QLabel* m_lockLabel;

    KeyEventHandler* m_keyEventHandler;
    FeedbackPlayer* m_feedbackPlayer;
    QEvent m_mouseEventType;
    bool m_lockEnabled;
};

#endif // PIEWIDGET_H
